﻿//-----------------------------------------------------------------------
// <copyright file="TreasureMapPage.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.ViewModels
{
    using System;
    using System.Globalization;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.ApplicationModel.DataTransfer;
    using Windows.Storage.Streams;

    /// <summary>
    /// View Model Representation of the Treasure Map Page.
    /// </summary>
    public class TreasureMapPage : IShare
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TreasureMapPage" /> class.
        /// </summary>
        public TreasureMapPage()
        {
            this.GoToCommand = new Command<string>(this.GoToCategory);
            this.GoBack = new Command(() =>
                {
                    Navigation.GoBack();
                });

            this.LaunchSearch = new Command(() =>
                {
                    Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().Show();
                });

            DataTransferManager.GetForCurrentView().DataRequested += this.TreasureMapPage_DataRequested;
        }

        /// <summary>
        /// Gets the command to go back.
        /// </summary>
        /// <value>The go back.</value>
        public Command GoBack { get; private set; }

        /// <summary>
        /// Gets or sets the command to navigate to a particular piece of guidance.
        /// </summary>
        /// <value>The go to command.</value>
        public Command<string> GoToCommand { get; set; }

        /// <summary>
        /// Gets or sets the launch search.
        /// </summary>
        /// <value>The launch search.</value>
        public Command LaunchSearch { get; set; }

        /// <summary>
        /// Gets the guidance completed.
        /// </summary>
        /// <value>The guidance completed.</value>
        public string GuidanceCompleted
        {
            get
            {
                return this.GetCompleted(3);
            }
        }

        /// <summary>
        /// Gets the prepare completed.
        /// </summary>
        /// <value>The prepare completed.</value>
        public string PrepareCompleted
        {
            get
            {
                return this.GetCompleted(1);
            }
        }

        /// <summary>
        /// Gets the quick intro completed.
        /// </summary>
        /// <value>The quick intro completed.</value>
        public string QuickIntroCompleted
        {
            get
            {
                return this.GetCompleted(2);
            }
        }

        /// <summary>
        /// Gets the tooling completed.
        /// </summary>
        /// <value>The tooling completed.</value>
        public string ToolingCompleted
        {
            get
            {
                return this.GetCompleted(4);
            }
        }

        /// <summary>
        /// Gets the workshops completed.
        /// </summary>
        /// <value>The workshops completed.</value>
        public string WorkshopsCompleted
        {
            get
            {
                return this.GetCompleted(5);
            }
        }

        /// <summary>
        /// event handler to navigate to the selected Category page.
        /// </summary>
        /// <param name="category">string representation of the Category to navigate to.</param>
        public void GoToCategory(string category)
        {
            var categoryValue = Convert.ToInt32(category, CultureInfo.InvariantCulture);
            if (categoryValue < 10000)
            {
                Navigation.Navigate(typeof(Views.Category), categoryValue);
            }
            else
            {
                switch (categoryValue)
                {
                    case 10000: // Favourites
                        {
                            Navigation.Navigate(typeof(Views.Favourites));
                            break;
                        }

                    case 10001: // News
                        {
                            Navigation.Navigate(typeof(Views.News));
                            break;
                        }
                }
            }
        }

        /// <summary>
        /// De-registers the share handler
        /// </summary>
        public void Unregister()
        {
            DataTransferManager.GetForCurrentView().DataRequested -= this.TreasureMapPage_DataRequested;
        }

        /// <summary>
        /// Event handler for the share event.
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="args">The parameter is not used.</param>
        public void TreasureMapPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var shareTextHeader = "ALM Readiness Treasure Map " + Environment.NewLine;

            var shareContent = @"Downloads: 
App in Windows Store http://aka.ms/vsartmapapp
Source code in CodePlex http://aka.ms/vsartmap";

            args.Request.Data.SetText(shareTextHeader + shareContent);

            // For email sharing
            args.Request.Data.Properties.Title = "ALM Readiness Treasure Map";
            args.Request.Data.Properties.Description = "The ALM Readiness treasure map sample application provides a catalogue (treasure map) of the available ALM readiness content, guiding you through the process of becoming proficient in ALM practices for Visual Studio.";

            var htmlExample = string.Format(CultureInfo.InvariantCulture, "<img src='{0}'></img><br>{1}<br>", "Image.png", shareContent.Replace(Environment.NewLine, "<br>"));

            args.Request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(htmlExample));

            // Because the HTML contains a local image, we need to add it to the ResourceMap.
            var streamRef = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Tile.Scale-140.png"));
            args.Request.Data.ResourceMap["Image.jpg"] = streamRef;
        }

        /// <summary>
        /// Gets the completed.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <returns>System.String.</returns>
        private string GetCompleted(int category)
        {
            return string.Format(CultureInfo.CurrentUICulture, "{0} of {1}", App.Database.StatusManager.GetCompletedProjectCount(category), App.Database.StatusManager.GetProjectCount(category));
        }
    }
}